#include <iostream>
#include <string>
#include <signal.h>

void PrintPending(sigset_t pending)
{
    std::cout<< getpid() <<" process pending: ";
    for(int signo=31; signo>0; --signo)
    {
        //该信号在pending中打印1，否则打印0
        if(sigismember(&pending, signo)){
            std::cout<< 1 << " ";
        } else{
            std::cout<< 0 << " ";
        }
    }
    std::cout<<std::endl;
}
int main(int argc, char* argv[])
{
    //1.屏蔽信号
    sigset_t block_set, old_set;
    //1.1将block_set和old_set比特位清0
    sigemptyset(&block_set);
    sigemptyset(&old_set);
    //1.2将2号信号添加到block_set中
    sigaddset (&block_set, 2);
    //1.3将block_set中的信号添加到进程的block
    sigprocmask(SIG_BLOCK, &block_set, &old_set);

    //2.从程序开始到结束一直获取pending位图
    sigset_t pending;
    int cnt = 10;
    while(true)
    {
        //2.1获取当前进程的pending位图
        sigpending(&pending);
        //2.2打印获取到的位图
        PrintPending(pending);
        sleep(2);

        //10秒后解除对2号新号的屏蔽
        if(cnt == 0)
        {
            //将旧的block赋值给新的block，就解除了对2号信号的屏蔽
            sigprocmask(SIG_SETMASK, &old_set, &block_set);
        }
        --cnt;
    }
    
    return 0;
}
